package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.PageResult;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.entity.Tag;
import com.itheima.ydd.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Copyright (C), 2021-2022
 * Author: 16372
 * Date: 2022/2/25 17:05
 * FileName: TagController
 * Description: 标签管理
 */
@RestController
@RequestMapping("/ydd_tag")
public class TagController {
    @Autowired
    private TagService tagService;
    
    /**
     * @Author 16372
     * @Description //TODO 标签分类分页查询
     * @Date 18:14 2022/2/25
     * @Param [tagname, page, limit] 标签名 当前页 每页显示条数
     * @return com.itheima.ydd.common.ResultData
     **/
    @GetMapping("/list")
    public ResultData getList(String tagname,
                              Integer page,
                              Integer limit){
        Page<Tag> tagPage = new Page<>(page, limit);
        tagService.page(tagPage,new LambdaQueryWrapper<Tag>()
                .like(tagname != null,Tag::getTagname,tagname));

       /* PageResult pageResult = PageResult.init(tagPage.getTotal()
                , tagPage.getSize()
                , tagPage.getCurrent()
                , tagPage.getRecords());*/
        return ResultData.ok("",PageResult.init(tagPage));
    }

    /**
     * @Author 16372
     * @Description //TODO 标签详情查询
     * @Date 18:24 2022/2/25
     * @Param [id] 查询条件 [必须]
     * @return com.itheima.ydd.common.ResultData
     **/
    @GetMapping("/rowInfo")
    public ResultData getRowInfo(Integer id){
        Tag tag = tagService.getById(id);
        return ResultData.ok("",tag);
    }

    /**
     * @Author 16372
     * @Description //TODO 修改标签
     * @Date 18:58 2022/2/25
     * @Param [tag] 修改的标签
     * @return com.itheima.ydd.common.ResultData
     **/
    @PostMapping("/update")
    public ResultData update(@RequestBody Tag tag){
        tagService.updateById(tag);
        return ResultData.ok("","");
    }

    /**
     * @Author 16372
     * @Description //TODO 新增标签
     * @Date 19:19 2022/2/25
     * @Param [tag]
     * @return com.itheima.ydd.common.ResultData
     **/
    @PostMapping("/add")
    public ResultData add(@RequestBody Tag tag){
        List<Tag> list = tagService.list(new LambdaQueryWrapper<Tag>().eq(Tag::getTagname, tag.getTagname()));
        if (null == list || list.size() == 0){
            tagService.save(tag);
            return ResultData.ok("","");
        }
        return ResultData.error("该标签已存在,不能重复添加");
    }

    @GetMapping("/del")
    public ResultData delById(Integer id){
        tagService.removeById(id);
        return ResultData.ok("","");
    }

    /**
     * @Author 16372
     * @Description //TODO 获取全部标签数据
     * @Date 19:47 2022/2/25
     * @Param []
     * @return com.itheima.ydd.common.ResultData
     **/
    @GetMapping("/all")
    public ResultData getAll(){
        List<Tag> tagList = tagService.list();
        return ResultData.ok("",tagList);
    }


}
